package com.huike.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huike.core.controller.BaseController;
import com.huike.core.domain.AjaxResult;
import com.huike.core.page.TableDataInfo;
import com.huike.dto.TallyChannelDto;
import com.huike.dto.TallyDto;
import com.huike.dto.domain.Activity;
import com.huike.dto.domain.Tally;
import com.huike.pojo.BusinessOpportunity;
import com.huike.pojo.Contract;
import com.huike.service.ClueService;
import com.huike.service.ContractService;
import com.huike.service.IActivityService;
import com.huike.service.TallyService;
import com.huike.vo.TallyCustomerVO;
import com.huike.vo.VulnerabilityMapVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping
@RestController
@Api(tags = "统计分析的接口")
public class TallyController extends BaseController {
    @Autowired
    private TallyService tallyService;
    @Autowired
    private ClueService clueService;
    @Autowired
    private IActivityService activityService;
    @Autowired
    private ContractService contractService;

    @ApiOperation("客户统计-分页接口")
    @GetMapping("/report/contractStatisticsList")
    public TableDataInfo list(Tally tally) {
        startPage();
        List<Tally> list = tallyService.contractStatisticsList(tally);
        return getDataTable(list);
    }

    @ApiOperation("客户统计-查询全部课程")
    @GetMapping("/clues/course/listselect")
    public AjaxResult listSelect() {
        return success(tallyService.listSelect());
    }

    @ApiOperation("部门统计-分页接口")
    @GetMapping("/report/deptStatisticsList/{startDate}/{endDate}")
    public TableDataInfo deptStatisticsList(
            @ApiParam(name = "startDate", value = "开始日期", required = true)
            @PathVariable Date startDate,
            @ApiParam(name = "endDate", value = "结束日期", required = true)
            @PathVariable Date endDate,
            TallyDto tallyDto) {
        startPage();
        // 将日期参数设置到 TallyDto 中，供服务层使用
        tallyDto.setStartDate(startDate);
        tallyDto.setEndDate(endDate);
        List<TallyDto> list = tallyService.deptStatisticsList(tallyDto);
        return getDataTable(list);
    }

    @ApiOperation("线索统计-分页接口")
    @GetMapping("/report/cluesStatisticsList")
    public TableDataInfo cluesStatisticsList(TallyDto tallyDto) {
        startPage();
        List<BusinessOpportunity> clueList = clueService.list();
        List<TallyDto> list = new ArrayList<>();
        clueList.forEach(clue -> {
            TallyDto dto = new TallyDto();
            Activity one = activityService.getOne(Wrappers.<Activity>lambdaQuery().eq(Activity::getId, clue.getActivityId()));
            BeanUtils.copyProperties(clue, dto);
            dto.setClueCreateTime(clue.getCreateTime());
            dto.setClueStatus(clue.getStatus());
            if (one != null) {
                BeanUtils.copyProperties(one, dto);
                Contract contract = contractService.getOne(Wrappers.<Contract>lambdaQuery().eq(Contract::getActivityId, one.getId()));
                if (contract != null) {
                    BeanUtils.copyProperties(contract, dto);
                }
            }


            list.add(dto);
        });
        return getDataTable(list);
    }

    @ApiOperation("渠道统计-分页接口")
    @GetMapping("/report/activityStatisticsList")
    public TableDataInfo activityStatisticsList(TallyChannelDto tallyChannelDto) {
        startPage();
        List<TallyChannelDto> list = tallyService.activityStatisticsList(tallyChannelDto);
        return getDataTable(list);
    }

    /**
     * 漏斗图数据
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @GetMapping("/report/getVulnerabilityMap/{beginCreateTime}/{endCreateTime}")
    public AjaxResult getVulnerabilityMap(@PathVariable String beginCreateTime, @PathVariable String endCreateTime) {
        VulnerabilityMapVo vulnerabilityMapDTO = tallyService.getVulnerabilityMap(beginCreateTime, endCreateTime);
        return AjaxResult.success(vulnerabilityMapDTO);
    }

    @ApiOperation("客户统计-波形图")
    @GetMapping("/report/contractStatistics/{beginCreateTime}/{endCreateTime}")
    public TallyCustomerVO contractStatistics(@PathVariable String beginCreateTime, @PathVariable String endCreateTime){
       return tallyService.contractStatistics(beginCreateTime,endCreateTime);
    }
    @ApiOperation("销售统计-波形图")
    @GetMapping("/report/salesStatistics/{beginCreateTime}/{endCreateTime}")
    public TallyCustomerVO salesStatistics(@PathVariable String beginCreateTime, @PathVariable String endCreateTime){
        return tallyService.salesStatistics(beginCreateTime,endCreateTime);
    }

    @ApiOperation("线索统计-波形图")
    @GetMapping("/report/cluesStatistics/{beginCreateTime}/{endCreateTime}")
    public TallyCustomerVO cluesStatistics(@PathVariable String beginCreateTime, @PathVariable String endCreateTime){
        return tallyService.cluesStatistics(beginCreateTime,endCreateTime);
    }@ApiOperation("渠道统计-波形图")
    @GetMapping("/report/chanelStatistics/{beginCreateTime}/{endCreateTime}")
    public TallyCustomerVO chanelStatistics(@PathVariable String beginCreateTime, @PathVariable String endCreateTime){
        return tallyService.cluesStatistics(beginCreateTime,endCreateTime);
    }    @GetMapping("/report/activityStatistics/{beginCreateTime}/{endCreateTime}")
    public TallyCustomerVO activityStatistics(@PathVariable String beginCreateTime, @PathVariable String endCreateTime){
        return tallyService.cluesStatistics(beginCreateTime,endCreateTime);
    }
}
